﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using Telerik.Web.UI;
using MLM.AdminCP;
using MLM.Base;

namespace MLM
{
    public partial class ProductInStockMananger : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Permission.CheckIsAdmin();
            RadProductInStocksGrid.ItemUpdated += new GridUpdatedEventHandler(radProductInStockGrid_ItemUpdated);
            RadProductInStocksGrid.ItemInserted += new GridInsertedEventHandler(radProductInStockGrid_ItemInserted);
            RadProductInStocksGrid.ItemDeleted += new GridDeletedEventHandler(radProductInStockGrid_ItemDeleted);
            RadProductInStocksGrid.ItemCommand += new GridCommandEventHandler(RadProductInStocksGrid_ItemCommand);
        }

        void RadProductInStocksGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
             if(e.CommandName == RadGrid.InitInsertCommandName)
             {
                 e.Canceled = true;
                 var newValues = new
                     System.Collections.Specialized.ListDictionary();
                 newValues["IsShowOnShop"] = false;
                 e.Item.OwnerTableView.InsertItem(newValues);
             }
        }
        void radProductInStockGrid_ItemDeleted(object sender, GridDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                Message.ShowError("ProductInStock " + e.Item["ProductInStockID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("ProductInStock " + e.Item["ProductInStockID"].Text + " deleted!");
            }
        }

        void radProductInStockGrid_ItemInserted(object sender, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                Message.ShowError("ProductInStock cannot be inserted. Reason: " + e.Exception.Message);
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
            else
            {
                Message.ShowSuccess("ProductInStock inserted!");
            }
        }

        void radProductInStockGrid_ItemUpdated(object sender, GridUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                Message.ShowError("ProductInStock " + e.Item["ProductInStockID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("ProductInStock " + e.Item["ProductInStockID"].Text + " updated.");
            }
        }   
    }
}